#include "led_light.h"
#include "pwm.h"
#include "OSAL.h"
#include "gpio.h"
#include "version.h"
//#include "app_led.h"
#include "error.h"

#define GPIO_RED      P26
#define GPIO_GREEN    P27
#define GPIO_BLUE     P31
#define GPIO_WHITE	  P32
#define GPIO_YELLOW	  P33


static uint16_t s_light[3];
static light_blink_cfg_t s_lightBlink;

static void light_start_timer(void)
{
    //osal_start_timerEx(bleMesh_TaskID, TIMER_LIGHT_EVT, 30*1000);
}
static void light_stop_timer(void)
{
    //osal_stop_timerEx(bleMesh_TaskID, TIMER_LIGHT_EVT);
}


void light_reflash(void)
{
    if(s_light[LIGHT_RED] + s_light[LIGHT_GREEN] + s_light[LIGHT_BLUE])
    {
        HalPwmCloseChannel(PWM_CH0);
        HalPwmDestroy(PWM_CH0);
        HalPwmCloseChannel(PWM_CH1);
        HalPwmDestroy(PWM_CH1);
        HalPwmCloseChannel(PWM_CH2);
        HalPwmDestroy(PWM_CH2);
        HalPwmStop();
        HalGpioPinInit(GPIO_GREEN, GPIO_INPUT);
        HalGpioPinInit(GPIO_RED, GPIO_INPUT);
        HalGpioPinInit(GPIO_BLUE, GPIO_INPUT);
        HalGpioPupdConfig(GPIO_GREEN, GPIO_PULL_UP);
        HalGpioPupdConfig(GPIO_RED, GPIO_PULL_UP);
        HalGpioPupdConfig(GPIO_BLUE, GPIO_PULL_UP);

        if(s_light[LIGHT_GREEN])
        {
            HalPwmInit(PWM_CH0, PWM_CLK_DIV_64, PWM_CNT_UP, PWM_POLARITY_RISING);
            HalPwmSetCountVal(PWM_CH0, s_light[LIGHT_GREEN], LIGHT_TOP_VALUE);
            HalPwmOpenChannel(PWM_CH0, GPIO_GREEN);
        }

        if(s_light[LIGHT_BLUE])
        {
            HalPwmInit(PWM_CH1, PWM_CLK_DIV_64, PWM_CNT_UP, PWM_POLARITY_RISING);
            HalPwmSetCountVal(PWM_CH1, s_light[LIGHT_BLUE], LIGHT_TOP_VALUE);
            HalPwmOpenChannel(PWM_CH1, GPIO_BLUE);
        }

        if(s_light[LIGHT_RED])
        {
            HalPwmInit(PWM_CH2, PWM_CLK_DIV_64, PWM_CNT_UP, PWM_POLARITY_RISING);
            HalPwmSetCountVal(PWM_CH2, s_light[LIGHT_RED], LIGHT_TOP_VALUE);
            HalPwmOpenChannel(PWM_CH2, GPIO_RED);
        }

        HalPwmStart();
        light_stop_timer();
        light_start_timer();
    }
    else
    {
        HalPwmCloseChannel(PWM_CH0);
        HalPwmDestroy(PWM_CH0);
        HalPwmCloseChannel(PWM_CH1);
        HalPwmDestroy(PWM_CH1);
        HalPwmCloseChannel(PWM_CH2);
        HalPwmDestroy(PWM_CH2);
        HalPwmStop();
        HalGpioPinInit(GPIO_GREEN, GPIO_INPUT);
        HalGpioPinInit(GPIO_RED, GPIO_INPUT);
        HalGpioPinInit(GPIO_BLUE, GPIO_INPUT);
        HalGpioPupdConfig(GPIO_GREEN, GPIO_PULL_UP);
        HalGpioPupdConfig(GPIO_RED, GPIO_PULL_UP);
        HalGpioPupdConfig(GPIO_BLUE, GPIO_PULL_UP);
        light_stop_timer();
    }
}

void light_timeout_handle(void)
{
    s_light[0] = 0;
    s_light[1] = 0;
    s_light[2] = 0;
    HalPwmCloseChannel(PWM_CH0);
    HalPwmDestroy(PWM_CH0);
    HalPwmCloseChannel(PWM_CH1);
    HalPwmDestroy(PWM_CH1);
    HalPwmCloseChannel(PWM_CH2);
    HalPwmDestroy(PWM_CH2);
    HalPwmStop();
    HalGpioPinInit(GPIO_GREEN, GPIO_INPUT);
    HalGpioPinInit(GPIO_RED, GPIO_INPUT);
    HalGpioPinInit(GPIO_BLUE, GPIO_INPUT);
    HalGpioPupdConfig(GPIO_GREEN, GPIO_PULL_UP);
    HalGpioPupdConfig(GPIO_RED, GPIO_PULL_UP);
    HalGpioPupdConfig(GPIO_BLUE, GPIO_PULL_UP);
}

int light_config(uint8_t ch, uint16_t value)
{
    if(ch >2 || (value > LIGHT_TOP_VALUE))
        return ERR_INVALID_PARAM;

    s_light[ch] = (uint16_t)value;
    return ERR_NONE;
}
int light_ctrl(uint8_t ch, uint16_t value)
{
    if(ch >2 || (value > LIGHT_TOP_VALUE))
        return ERR_INVALID_PARAM;

    s_light[ch] = (uint16_t)value;
    light_reflash();
    return ERR_NONE;
}


int light_init(void)
{
    s_light[LIGHT_GREEN] = 0;
    s_light[LIGHT_BLUE] = 0;
    s_light[LIGHT_RED] = 0;
	HalGpioPinInit(GPIO_RED, GPIO_INPUT);
    HalGpioPinInit(GPIO_GREEN, GPIO_INPUT);
    HalGpioPinInit(GPIO_BLUE, GPIO_INPUT);
	HalGpioPinInit(GPIO_WHITE, GPIO_INPUT);
    HalGpioPinInit(GPIO_YELLOW, GPIO_INPUT);
    
    HalGpioPupdConfig(GPIO_RED, WEAK_PULL_UP);
	HalGpioPupdConfig(GPIO_GREEN, WEAK_PULL_UP);
    HalGpioPupdConfig(GPIO_BLUE, WEAK_PULL_UP);
	HalGpioPupdConfig(GPIO_WHITE, WEAK_PULL_UP);
    HalGpioPupdConfig(GPIO_YELLOW, WEAK_PULL_UP);
	
    osal_memset(&s_lightBlink, 0, sizeof(s_lightBlink));
    s_lightBlink.val0=LIGHT_TURN_OFF;
    s_lightBlink.val1=LIGHT_TURN_ON;
    return ERR_NONE;
}

int light_blink_evt_cfg(uint8_t task_id,uint16_t event_id)
{
    if(s_lightBlink.status==0)
    {
        s_lightBlink.task_id=task_id;
        s_lightBlink.event_id=event_id;
        return ERR_NONE;
    }
    else
    {
        return ERR_BUSY;
    }
}
int light_blink_set(uint8_t light,uint8 blinkIntv,uint8 blinkCnt)
{
    if(s_lightBlink.status==0)
    {
        s_lightBlink.light=light;
        s_lightBlink.tagCnt=blinkCnt;
        s_lightBlink.intv = blinkIntv;
        s_lightBlink.status=1;

        if(s_lightBlink.task_id>0 && s_lightBlink.event_id>0)
        {
            light_ctrl(LIGHT_RED,0);
            light_ctrl(LIGHT_GREEN,0);
            light_ctrl(LIGHT_BLUE,0);
            s_lightBlink.curCnt=0;
            osal_set_event( s_lightBlink.task_id, s_lightBlink.event_id);
        }
        else
        {
            return ERR_NOT_FOUND;
        }

        return ERR_NONE;
    }
    else
    {
        return ERR_BUSY;
    }
}
void light_blink_porcess_evt(void)
{
    if(s_lightBlink.curCnt==(s_lightBlink.tagCnt*2) )
    {
        light_ctrl(LIGHT_RED,0);
        light_ctrl(LIGHT_GREEN,0);
        light_ctrl(LIGHT_BLUE,0);
        osal_stop_timerEx( s_lightBlink.task_id, s_lightBlink.event_id);
        s_lightBlink.status=0;
    }
    else
    {
        if(s_lightBlink.curCnt&0x01)
        {
            light_ctrl(s_lightBlink.light,s_lightBlink.val1);
        }
        else
        {
            light_ctrl(s_lightBlink.light,s_lightBlink.val0);
        }

        s_lightBlink.curCnt++;
        osal_start_timerEx(s_lightBlink.task_id, s_lightBlink.event_id,s_lightBlink.intv*100);
    }
}
